package com.ufo.chr;


import java.nio.charset.StandardCharsets;

public class CharAndByte {
    public static void main(String[] args){
        String str="和平";
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        System.out.println((char)Integer.valueOf(0x6C49).intValue());

        for(int i=0;i<str.length();i++){
            System.out.print(str.codePointAt(i)+"|");
        }
        System.out.println("字符串的长度"+str.length());
        for(int i=0;i<str.length();i++){
            System.out.print(Integer.toHexString(str.codePointAt(i))+"|");
        }
        System.out.println("字符串的code 转16进制");
        for(int i=0;i<bytes.length;i++){
            System.out.print(bytes[i]+"|");
        }
        System.out.println("bytes的长度"+bytes.length);

        StringBuilder sb=new StringBuilder();
        for(int i=0;i<bytes.length;i++){
            //& 0xff 将补码转为原码
            if ((bytes[i] & 0xff) < 0x10){//补0
                sb.append("0");
            }
            sb.append(Long.toString(bytes[i] & 0xff,10));
            System.out.print(Long.toString(bytes[i],16)+"|");
        }
        System.out.println(sb);
        toBinary(bytes);
    }

    public static String toBinary(byte[] bytes){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<bytes.length;i++){
            String b=Integer.toBinaryString(bytes[i]&0xFF+0x100);
            System.out.println(b);
            sb.append(b);
        }
        return sb.toString();
    }
}
